import { execSync } from "child_process"
import { createHash } from 'crypto'
const secretMap = [
  {
    appid: 'month',
    secret: '202cb962ac59075b964b07152d234b70'
  }
]

const phoneWithMap = ['10000','10010','10086']
export function validateDigest(appid: string, digest: string) {
  
    if(!appid || !digest) return false;
    const map = secretMap.find(item => item.appid === appid);
    if(!map) return false;
    const md5String = map.appid + map.secret;
    const md5 = createHash('md5').update(md5String, 'utf-8').digest('hex');
    return md5 === digest;
}

export function fail(message: string, status = 500) {
  return { success: false, message: message, status};
}
export function sendSms(phone: string, content: string) {
  // 模拟发送短信
  // termux-sms-send -n 10010 你好
  try {
    const res = execSync(`termux-sms-send -n ${phone} ${content}`);
    return {
      success: true,
      message: res.toString(),
    };
  } catch (error: any) {
    return {
      success: false,
      message: error.stderr.toString()
    };
  }
}
export function isPhone(phone: string) {
  if(phoneWithMap.includes(phone)) {
    return true
  }
  return /^1[3456789]\d{9}$/.test(phone) 
}